package com.yanqu.road.server.cmd.activity.dragonboatrace;

import com.yanqu.road.entity.activity.dragonboatrace.config.DragonBoatRacePlaceConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDragonBoatRaceUnionReward;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.dragonboatrace.DragonBoatRaceProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dragonboatrace.DragonBoatRaceModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.dragonboatrace.DragonBoatRaceMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_DRAGONBOATRACE_RECEIVE_UNION_REWARD_FROM_CROSS, desc = "赛龙舟领取联盟奖励   跨服返回区服 协议")
public class DragonBoatRaceReceiveUnionRewardFromCrossCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardCrossToGameMsg crossToGameMsg = DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardCrossToGameMsg.parseFrom(packet.getBytes());
        DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardRespMsg.Builder stcMsg = crossToGameMsg.getStcMsg().toBuilder();
        if (stcMsg.getRet() == 0) {
            DragonBoatRacePlaceConfig placeConfig = DragonBoatRaceMgr.getConfig().getPlaceConfigMap().get(crossToGameMsg.getPlaceId());
            // 领取奖励
            CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
            Property reward = PropertyHelper.parseStringToProperty(placeConfig.getRewards());
            currencyModule.addCurrency(reward, eLogMoneyType.DragonBoatRaceActivity, eLogMoneyType.DragonBoatRaceActivityReceiveUnionReward);

            stcMsg.setReward(placeConfig.getRewards());
            DragonBoatRaceModule dragonBoatRaceModule = DragonBoatRaceMgr.getDragonBoatRaceModule(player);

            dragonBoatRaceModule.dragonBoatRaceUser.baseData.setPlaceId(crossToGameMsg.getPlaceId());
            dragonBoatRaceModule.synRedPoint(crossToGameMsg.getSynUnionInfo());

//            LogMgr.addLogDragonBoatRaceUnionReward(new LogDragonBoatRaceUnionReward(DragonBoatRaceMgr.getActivityInfo().getActivityId(), player.getUserInfo().getUnionUid(), dragonBoatRaceModule.dragonBoatRaceUser.baseData.getUserId(), crossToGameMsg.getPlaceId(), System.currentTimeMillis()));
            AutoLogMgr.add(new LogDragonBoatRaceUnionReward(DragonBoatRaceMgr.getActivityInfo().getActivityId(), player.getUserInfo().getUnionUid(), dragonBoatRaceModule.dragonBoatRaceUser.baseData.getUserId(), crossToGameMsg.getPlaceId(), System.currentTimeMillis()));
        }
        player.sendPacket(ClientProtocol.U_DRAGONBOATRACE_RECEIVE_UNION_REWARD, stcMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
